{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/38_cloud_geotiff.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "# How to use Cloud Optimized GeoTIFF with Earth Engine \n",
    "\n",
    "A Cloud Optimized GeoTIFF (COG) is a regular GeoTIFF file, aimed at being hosted on a HTTP file server, with an internal organization that enables more efficient workflows on the cloud. It does this by leveraging the ability of clients issuing HTTP GET range requests to ask for just the parts of a file they need. \n",
    "\n",
    "More information about COG can be found at <https://www.cogeo.org/in-depth.html>\n",
    "\n",
    "Some publicly available Cloud Optimized GeoTIFFs:\n",
    "\n",
    "* https://stacindex.org/\n",
    "* https://cloud.google.com/storage/docs/public-datasets/landsat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "## Loading a Cloud Optimized GeoTIFF as an ee.Image\n",
    "\n",
    "Note that only Cloud Optimized GeoTIFF hosted on Google Cloud Storage is supported. Other cloud storage providers (e.g., AWS) are not supported.\n",
    "\n",
    "In this example, we are going to use the [Planet Disaster Data](https://stacindex.org/catalogs/planet-labs-stac-catalog#/hQQtdnqXybvrLypHS1EX8TjGtfuU84GRBb)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "URL = \"https://storage.googleapis.com/pdd-stac/disasters/hurricane-harvey/0831/20170831_172754_101c_3B_AnalyticMS.tif\"\n",
    "# URL = 'gs://pdd-stac/disasters/hurricane-harvey/0831/20170831_172754_101c_3B_AnalyticMS.tif'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "image = geemap.load_GeoTIFF(URL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "vis = {\"bands\": [\"B3\", \"B2\", \"B1\"], \"min\": 3000, \"max\": 13500}\n",
    "\n",
    "Map.addLayer(image, vis, \"Cloud Image\")\n",
    "Map.centerObject(image.geometry(), 12)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {},
   "source": [
    "## Loading a list of Cloud Optimized GeoTIFFs as an ee.ImageCollection\n",
    "\n",
    "In this example, we are going to use the Landsat data hosted on Google Cloud. \n",
    "\n",
    "https://cloud.google.com/storage/docs/public-datasets/landsat\n",
    "\n",
    "https://console.cloud.google.com/storage/browser/gcp-public-data-landsat/LC08/01/044/034/LC08_L1TP_044034_20131228_20170307_01_T1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "B3 = \"gs://gcp-public-data-landsat/LC08/01/044/034/LC08_L1TP_044034_20131228_20170307_01_T1/LC08_L1TP_044034_20131228_20170307_01_T1_B3.TIF\"\n",
    "B4 = \"gs://gcp-public-data-landsat/LC08/01/044/034/LC08_L1TP_044034_20131228_20170307_01_T1/LC08_L1TP_044034_20131228_20170307_01_T1_B4.TIF\"\n",
    "B5 = \"gs://gcp-public-data-landsat/LC08/01/044/034/LC08_L1TP_044034_20131228_20170307_01_T1/LC08_L1TP_044034_20131228_20170307_01_T1_B5.TIF\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "URLs = [B3, B4, B5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "collection = geemap.load_GeoTIFFs(URLs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "image = collection.toBands().rename([\"Green\", \"Red\", \"NIR\"]).selfMask()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "vis = {\"bands\": [\"NIR\", \"Red\", \"Green\"], \"min\": 100, \"max\": 12000, \"gamma\": 0.8}\n",
    "\n",
    "Map.addLayer(image, vis, \"Image\")\n",
    "Map.centerObject(image.geometry(), 8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "ndvi = image.normalizedDifference([\"NIR\", \"Red\"])\n",
    "ndvi_vis = {\"min\": 0, \"max\": 1, \"palette\": [\"blue\", \"green\"]}\n",
    "Map.addLayer(ndvi, {}, \"NDVI\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
